Shader "dai/newDiffuse"
{
    Properties
    {
        _Color ("Color Tint", Color) = (1, 1, 1, 1)
        _MainTex ("Texture", 2D) = "white" {}
        _Specular ("Specular Color", Color) = (1, 1, 1, 1)
		_Gloss ("Gloss", Range(8.0, 256)) = 20
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" "Queue"="Geometry"}
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fwdbase	

            #include "UnityCG.cginc"
            #include "Lighting.cginc"
			#include "AutoLight.cginc"

            fixed4 _Color;
			sampler2D _MainTex;
			float4 _MainTex_ST;
            fixed4 _Specular;
			float _Gloss;

            struct appdata
            {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
                float2 diffAndSpecular:TEXCOORD1;
                SHADOW_COORDS(2)
            };

            v2f vert (appdata v)
            {
                v2f o;
                //o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);

                float4 worldPos4 = mul( unity_ObjectToWorld, v.vertex );
                o.pos = mul(UNITY_MATRIX_VP,worldPos4);

                float3 worldPos = worldPos4.xyz;
                float3 lightDir = normalize(UnityWorldSpaceLightDir(worldPos));
                float3 viewDir =  normalize(UnityWorldSpaceViewDir(worldPos));

                //fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
                float diffuseFactor = dot(v.normal,lightDir) * 0.5 + 0.5;
                float specularFactor = pow(max(dot(normalize((viewDir + lightDir) * 0.5),v.normal),0),_Gloss);
                o.diffAndSpecular = float2(diffuseFactor,specularFactor);

                TRANSFER_SHADOW(o);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 albedo = tex2D(_MainTex, i.uv);
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo.rgb;
                fixed3 diffuse = _Color.rgb * i.diffAndSpecular.x;
                fixed3 specular = _Specular.rgb * i.diffAndSpecular.y;
                fixed shadow = SHADOW_ATTENUATION(i);
                return fixed4(ambient + (diffuse + specular) * albedo.rgb * shadow,1.0);
                //return fixed4(shadow,shadow,shadow,1);
            }
            ENDCG
        }
    }
    FallBack "VertexLit"
}
